package cn.qu.v1.platform.aop;

import cn.qu.v1.platform.inject.ProxyBeanDef;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationTargetException;

/**
 * Created by zh on 17/1/17.
 */
public class Invoker {
    private Invoker invoker;
    private MethodProxy methodProxy;
    private ProxyBeanDef def;

    public Invoker(MethodProxy methodProxy) {
        this.methodProxy = methodProxy;
    }

    public Object invoke(Object target, Object... args) {
        if (methodProxy == null) {
            try {
                return def.getMethod().invoke(def.getObj(), invoker, target, args);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            try {
                return methodProxy.invokeSuper(target, args);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        return null;
    }

    public Invoker(Invoker invoker, ProxyBeanDef def) {
        this.invoker = invoker;
        this.def = def;
    }
}
